<?php
namespace Merchant\Controller;
use Think\Controller;
class LunboController extends CommController {
    public function index(){
        $m=M('lb');
        $n =M("business");
        $bid = $_GET['bid'];
        if($_GET['bid']){
            $bb = $n->field('name')->where("id = $bid")->select();
            foreach($bb as $key=>$value){
                $map[$key]['b.name'] = array('like','%'.$value['name'].'%');
                $map[$key]['g.name'] = array('like','%'.$_GET['selname'].'%');
            }
        }else{
            $map[]['g.name'] = array('like','%'.$_GET['selname'].'%');
        }

        $count = $m
            ->alias('l')
            ->field("g.name,b.name as bname,l.*")
            ->join('goods AS g ON l.gid = g.id  ')
            ->join('business AS b ON l.bid = b.id ')
            ->order('l.id desc')
            ->where($map)
            // ->page($curr, $this->limit)
            ->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $data = $m
            ->alias('l')
            ->field("g.name,b.name as bname,l.*")
            ->join('goods AS g ON l.gid = g.id  ')
            ->join('business AS b ON l.bid = b.id ')
            ->order('l.id desc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->where($map)
            ->select();
        $this->assign('data',$data);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',M("business")->select());
        $this->display(); // 输出模板
    }
    public function add()
    {
    
        if(IS_POST){
        $m=M('lb');
        $pic = $this->upload();
            if ($pic)
                $_POST['img'] = $pic['img']['savepath'].$pic['img']['savename'];
              $data=$_POST;
            $data['time']=time();
            $data['del'] = 1;

            if($m->add($data))
            {
                alert('添加成功');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }else{
                alert('添加失败');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }
            return;
        }else{
            $this->assign('list1',M('business')->select());
            $this->assign('list',M('goods')->select());
            $this->display();
        }
    }
 public function edit(){
       $m = M('lb');
       $id = $_GET['id'];
        if(IS_POST)
        {   
            $pic = $this->upload();
            if ($pic)
            $_POST['img'] = $pic['img']['savepath'].$pic['img']['savename'];
            $data=$_POST;
            $data['start'] = strtotime($data['start']);
            $data['stop'] = strtotime($data['stop']);
            if($m->save($data))
            {
                alert('修改成功');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }else{
                alert('修改失败');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
            }
        }else{
            $info = $m
                        ->alias('l')
                        ->field("g.name,b.name as bname,l.*")
                        ->join('goods AS g ON l.gid = g.id  ')
                        ->join('business AS b ON l.bid = b.id ')
                        ->where("l.id = $id")
                        ->find();
                     
            $this->assign('info',$info);
            $this->assign('list',M('goods')->where('state = 1')->select());
            $this->assign('list1',M('business')->where('state = 1')->select());
            $this->display();
        }
    }
    public function show(){
        $m = M('lb');
        $id = $_GET['id'];
        if($_GET['val']==1){
            $data['state'] = 0;
        }else{
            $data['state'] = 1;
        }
        $list = $m->where("id = $id")->save($data);
        $this->redirect('Lunbo/index');
    }
    public function del(){
        if (!$_GET['id']) return ;
        $db = M('lb');
        $data['del'] = 0;
        $data['deltime']=time();
        $list = $db->where('id='.$_GET['id'])->save($data);
        if($list)
        {
               alert('删除成功');
                echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }else{
            alert('删除失败');
         echo "<script>location.href='". $_SERVER['HTTP_REFERER'] ."'</script>";
        }
    }    
}